package com.bl.weilan.service.biz.companyinvester.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.CompanyInvesterEntity;
import com.bl.weilan.dao.repository.CompanyInvesterRepository;
import com.bl.weilan.service.biz.companyinvester.CompanyInvesterService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.companyinvester.CompanyInvesterAddVo;
import com.bl.weilan.service.vo.companyinvester.CompanyInvesterListVo;
import com.bl.weilan.service.vo.companyinvester.CompanyInvesterModVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class CompanyInvesterServiceImpl implements CompanyInvesterService {
    @Autowired
    CompanyInvesterRepository companyinvesterRepository;

    @Override
    public CompanyInvesterEntity getCompanyInvesterById(String companyinvesterId) {
        CompanyInvesterEntity companyinvesterEntity = companyinvesterRepository.findOne(companyinvesterId);
        return companyinvesterEntity;
    }

    @Override
    public CompanyInvesterListVo getCompanyInvesterLists( Pageable pageable ){
        Page<CompanyInvesterEntity> companyinvesterEntities;
        CompanyInvesterListVo companyinvesterListVo = new CompanyInvesterListVo();

        companyinvesterEntities = companyinvesterRepository.findAll(pageable);


        companyinvesterListVo.setTotalCount(companyinvesterEntities.getTotalElements());
        companyinvesterListVo.setList(companyinvesterEntities.getContent());

        return companyinvesterListVo;
    }

    @Override
    public void addCompanyInvester(CompanyInvesterAddVo companyinvesterAddVo) {
        CompanyInvesterEntity companyinvesterEntity = new CompanyInvesterEntity();
        companyinvesterEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(companyinvesterAddVo, companyinvesterEntity);
        companyinvesterRepository.save(companyinvesterEntity);

    }

    @Override
    public void del(DelBody id) {
        companyinvesterRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            companyinvesterRepository.delete(id);
        }
    }

    @Override
    public void modify(CompanyInvesterModVo companyinvesterModVo){
        CompanyInvesterEntity companyinvesterEntity = companyinvesterRepository.findOne(companyinvesterModVo.getId());

        if (companyinvesterEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        BeanUtils.copyProperties(companyinvesterModVo, companyinvesterEntity);
        companyinvesterRepository.save(companyinvesterEntity);
    }
}
